
//终端命令下发结果查询类
function VideoStatus(vehicleId,simNo,channel,stream,title)
{
	this.simNo = simNo;
	this.vehicleId = vehicleId;
	this.channel = channel;
	this.stream = stream;
	this.timerName = "timerForQuery"+simNo+"_"+ channel;
    this.times=60;//下发命令后，等待应答倒计时60秒
    this.onSuccess=null;
	this.title = title;
}

/**
 * 发送实时视频请求
 */
VideoStatus.prototype.sendRealVideoRequest = function ()
{
	var me = this;
	var params = {vehicleId:this.vehicleId,simNo : this.simNo, channel:this.channel, stream:this.stream};
    var url = globalConfig.webPath + "/videoCommand/realTimeVideo.action";
	$.getJSON(url, params, 
			function(result){
		if (result.success) {
			var commandId = result.data; //下发成功后，获取到命令Id
			me.startQueryResult(commandId);
		}else
		{
			me.setVideoChannelStatus("请求错误");
		}

	});
}

VideoStatus.prototype.onSuccess = function(result)
{
	this.setVideoChannelTitle(this.title);
}

VideoStatus.prototype.setVideoChannelStatus = function(message)
{
	var strSimNo =  this.simNo;
	if(strSimNo.length < 12)   
		strSimNo = "0" + strSimNo;
	MyVideoPlayer.getVideoHandler().setVideoInfo(strSimNo, this.channel, this.stream,
		this.title + " " + message);
}

VideoStatus.prototype.setVideoChannelTitle = function(title)
{
	var strSimNo =  this.simNo;
	if(strSimNo.length < 12)   
		strSimNo = "0" + strSimNo;
	MyVideoPlayer.getVideoHandler().setVideoTitle(strSimNo, this.channel, this.stream,
		title);
}

VideoStatus.prototype.startQueryResult = function(commandId,url, onSuccess)
{ 
	if(!url)
		 url = globalConfig.webPath + "/command/getCommandResult.action"; //查询命令的执行情况
	this.url = url;
	var me = this;
	this.times = 60;//获取查询结果的最大次数
	this.setVideoChannelStatus("正在请求");
	  //使用jquery.timer插件，进行定时调用，最多执行次数取决this.times的次数定义
	$('body').oneTime('500ms', this.timerName,function(){  
		 me.queryResult(commandId); //查询命令的返回结果
	});
	this.onSuccess = onSuccess;
}
//停止查询和倒计时
VideoStatus.prototype.stopQuery = function()
{
	$('body').stopTime (this.timerName);
}

//查询命令执行结果
VideoStatus.prototype.queryResult = function(commandId)
{	
    this.times--;
	var me = this;
	var params = {commandId:commandId};
	$.getJSON(this.url, params, 
			function(result){	
				/*
				author:shinion
				date:2019-01-08
				测试输出查询结果
				*/
				console.log("视频命令执行结果：");
				console.log(result);	
				  if(result.success == true)
				 {					
	　　　　　　　　me.setVideoChannelStatus("");
	                if(result.message != null && result.message.indexOf("成功") < 0 )
	                	me.setVideoChannelStatus(result.message);
	                else
	                    me.setVideoChannelTitle(me.title);
					if(me.onSuccess)
					{
						me.onSuccess(result);
					}
				}else {
				   me.setVideoChannelStatus(result.message);
				   if(me.times == 1)
				   {						 
						me.setVideoChannelStatus("命令执行超时，无应答,请重新操作");
					    $('body').stopTime ();  
				   }else if(result.message == "等待发送" && me.times <= 50)
					{
						me.setVideoChannelStatus("服务器没有下发");
					    $('body').stopTime ();  
					}
				   else{
					     //最多执行5次
						 $('body').oneTime('500ms', me.timerName,function(){  
								 me.queryResult(commandId); //查询命令的返回结果
						});
					}
					
				}
		  }
	  );
}